package com.gwtgeo.ui.client.mvp.searchform;

import java.util.Map;

import com.google.gwt.user.client.ui.HasText;
import com.gwtgeo.ui.client.mvp.adapter.HasTextFormItemAdapter;
import com.gwtgeo.ui.client.mvp.windowsmanager.WindowedDisplay;
import com.gwtgeo.ui.client.mvp.windowsmanager.WindowedView;
import com.smartgwt.client.types.HeaderControls;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.HasClickHandlers;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.VLayout;

public class SearchFormView extends WindowedView<VLayout> implements
		SearchFormPresenter.Display {

	private DynamicForm searchForm;

	private IButton searchButton;

	private TextItem textItem;

	public SearchFormView() {
		super();
	}

	public HasClickHandlers getSearchButtonClick() {
		return this.searchButton;
	}

	@Override
	public HasText getSearchString() {
		return new HasTextFormItemAdapter(textItem);
	}

	@Override
	public void clear() {
		this.textItem.clearValue();
	}

	@Override
	public boolean validate() {
		return searchForm.validate();
	}

	@Override
	protected VLayout buildWidget() {

		VLayout widget = new VLayout();

		this.searchForm = new DynamicForm();
		this.textItem = new TextItem("text");
		this.textItem.setWidth(200);
		this.textItem.setTitle("Location Name");
		this.textItem.setRequired(true);
		this.searchForm.setItems(this.textItem);

		this.searchButton = new IButton("Search");
		widget.addMember(this.searchForm);
		widget.addMember(this.searchButton);

		return widget;
	}

	@Override
	protected int getWindowHeight() {
		return 100;
	}

	@Override
	protected String getWindowTitle() {
		return "Search";
	}

	@Override
	protected int getWindowWidth() {
		return 300;
	}
	
	@Override
	protected void addHeaderControls(Map<String, HasClickHandlers> headers) {
		window.setHeaderControls(HeaderControls.HEADER_LABEL, headers
				.get(WindowedDisplay.MINIMIZE_HEADER));
	}

}
